from django.shortcuts import render

# Create your views here.
from rest_framework.authentication import TokenAuthentication
from rest_framework.pagination import PageNumberPagination
from rest_framework.permissions import IsAuthenticated

# from .models import *
from .serializer import *
from rest_framework import permissions, viewsets, mixins
from rest_framework.response import Response
from utils.errormsg import erromsg
from utils.status import CODE_0, CODE_1
from rest_framework.viewsets import ModelViewSet
from utils.pagination import MyPagePagination


class Project(ModelViewSet):
    queryset = Myproject.objects.all()
    serializer_class = ProjectSerializers
    permission_classes = [IsAuthenticated]
    pagination_class = MyPagePagination

    def new_project(self, request):
        user = request.user
        data = request.data
        # data["user"] = user.pk
        if self.queryset.filter(projectname=data.get("projectname"), user=user.id):
            return Response({**CODE_1, "msg": "项目名已存在"})
        serializer = self.serializer_class(data=data, context={"user": user})

        if not serializer.is_valid():
            return Response({**CODE_1, "msg": erromsg(serializer)})
        serializer.save()

        return Response({**CODE_0, "data": serializer.data})

    def projectlist(self, request):

        user = request.user
        queryset = self.queryset.filter(user=user.id)

        # queryset2 = self.queryset.filter(user=user.id)
        param = request.query_params
        projectname = param.get("projectname")
        testrunner = param.get("testrunner")
        if projectname:
            queryset = queryset.filter(projectname__contains=projectname)
        if testrunner:
            queryset = queryset.filter(testrunner__contains=testrunner)
        queryset = self.paginate_queryset(queryset)
        serializer = self.serializer_class(queryset, many=True)
        # print(serializer.data)
        data = self.get_paginated_response(serializer.data)
        # data = serializer.data
        return Response({**CODE_0, **data})

    def del_project(self, request, pk):
        user = request.user
        queryset = self.queryset.filter(user=user.id, id=pk)
        if not queryset:
            return Response({**CODE_1, "msg": "不存在"})
        queryset.delete()
        return Response({**CODE_0})

    def editproject(self, request, pk):
        user = request.user
        data = request.data
        queryset = self.queryset.filter(user=user.id, id=pk)
        if not queryset:
            return Response({**CODE_1, "msg": "不存在"})

        instance = queryset.get(id=pk)

        serializer = self.serializer_class(instance, data=data, context={"user": user})
        if not serializer.is_valid():
            return Response({**CODE_1, "msg": erromsg(serializer)})
        serializer.save()
        data = serializer.data
        return Response({**CODE_0, "data": data})

    def projectdetail(self, request, pk):
        user = request.user
        data = request.data
        qs = self.queryset.filter(id=pk)
        serializer = self.serializer_class(qs, context={"user": user}, many=True)
        data = serializer.data
        return Response({**CODE_0, "data": data})
